$(document).ready(function () {

    // 全局关闭ajax缓存
    $.ajaxSetup({cache: false});

    //-------绑定事件

    $(document).on('click', '.signUp', function () {
        showDialog('register');
    });
    $(document).on('click', '.signIn,.gotologin', function () {
        showDialog('login');
    });
    $(document).on('click', '.signOut', function () {
        $.get(base_url('api/logout'), function () {
            window.location.href=base_url('api/show_guide');
        });
    });
    $(document).on('click', '.forgetPassword', function () {
        showDialog('forgetPassword');
    });
    $(document).on('click', '.wantToSell', function () {
        showDialog('wantToSell');
    });
    $(document).on('click', '.wantToBuy', function () {
        showDialog('wantToBuy');
    });
    $(document).on('click', '.edit-commodity', function () {
        var array = [];
        array['commodityId'] = $(this).data('commodity-id');
        showDialog('editCommodity', array);
    });
    $(document).on('click', '.delete-commodity', function () {
        var commodityId = $(this).data('commodity-id');
        noty({
            type: 'warning',
            text: '确定要删除吗？',
            buttons: [
                {
                    addClass: 'btn btn-sm btn-danger', text: '确定',
                    onClick: function ($noty) {
                        $noty.close();
                        $.ajax({
                            url: base_url('api/deleteCommodity'),
                            type: 'post',
                            data: {commodity_id: commodityId},
                            beforeSend: function () {
                                showProgressNoty();
                            },
                            complete: function () {
                                closeProgressNoty();
                            },
                            success: function (jsonrsp) {
                                try {
                                    var rsp = $.parseJSON(jsonrsp);
                                    noty({type: rsp.type, text: rsp.message, callback: {
                                        afterClose: function () {
                                            switch (rsp.type) {
                                                case 'success':
                                                    location.reload();
                                                    break;
                                                default:
                                                    break;
                                            }
                                        }
                                    }});
                                } catch (ex) {
                                    noty({type: 'error', text: '发生内部错误，请联系管理员'});
                                    throw ex;
                                }
                            }
                        });
                    }
                },
                {
                    addClass: 'btn btn-sm btn-default', text: '取消',
                    onClick: function ($noty) {
                        $noty.close();
                    }
                }
            ]
        });
    });
    $(document).on('click', '.sell-out', function () {
        var commodityId = $(this).data('commodity-id');
        $.ajax({
            url: base_url('api/sellOutCommodity'),
            type: 'post',
            data: {commodity_id: commodityId},
            beforeSend: function () {
                showProgressNoty();
            },
            complete: function () {
                closeProgressNoty();
            },
            success: function (jsonrsp) {
                try {
                    var rsp = $.parseJSON(jsonrsp);
                    noty({type: rsp.type, text: rsp.message, callback: {
                        afterClose: function () {
                            switch (rsp.type) {
                                case 'success':
                                    location.reload();
                                    break;
                                default:
                                    break;
                            }
                        }
                    }});
                } catch (ex) {
                    noty({type: 'error', text: '发生内部错误，请联系管理员'});
                    throw ex;
                }
            }
        });
    });
    $(document).on('click', '.put-top-commodity', function () {
        var commodityId = $(this).data('commodity-id');
        $.ajax({
            url: base_url('api/putTopCommodity'),
            type: 'post',
            data: {commodity_id: commodityId},
            beforeSend: function () {
                showProgressNoty();
            },
            complete: function () {
                closeProgressNoty();
            },
            success: function (jsonrsp) {
                try {
                    var rsp = $.parseJSON(jsonrsp);
                    noty({type: rsp.type, text: rsp.message, callback: {
                        afterClose: function () {
                            switch (rsp.type) {
                                case 'success':
                                    location.reload();
                                    break;
                                default:
                                    break;
                            }
                        }
                    }});
                } catch (ex) {
                    noty({type: 'error', text: '发生内部错误，请联系管理员'});
                    throw ex;
                }
            }
        });
    });
    $(document).on('click', '.edit-require', function () {
        var array = [];
        array['commodityId'] = $(this).data('commodity-id');
        showDialog('editRequire', array);
    });
    $(document).on('click', '.delete-require', function () {
        var commodityId = $(this).data('commodity-id');
        noty({
            type: 'warning',
            text: '确定要删除吗？',
            buttons: [
                {
                    addClass: 'btn btn-sm btn-danger', text: '确定',
                    onClick: function ($noty) {
                        $noty.close();
                        $.ajax({
                            url: base_url('api/deleteRequire'),
                            type: 'post',
                            data: {commodity_id: commodityId},
                            beforeSend: function () {
                                showProgressNoty();
                            },
                            complete: function () {
                                closeProgressNoty();
                            },
                            success: function (jsonrsp) {
                                try {
                                    var rsp = $.parseJSON(jsonrsp);
                                    noty({type: rsp.type, text: rsp.message, callback: {
                                        afterClose: function () {
                                            switch (rsp.type) {
                                                case 'success':
                                                    location.reload();
                                                    break;
                                                default:
                                                    break;
                                            }
                                        }
                                    }});
                                } catch (ex) {
                                    noty({type: 'error', text: '发生内部错误，请联系管理员'});
                                    throw ex;
                                }
                            }
                        });
                    }
                },
                {
                    addClass: 'btn btn-sm btn-default', text: '取消',
                    onClick: function ($noty) {
                        $noty.close();
                    }
                }
            ]
        });
    });

    //重发邮件
    $(document).on('click', '#ok-resendemail', function () {
        var user_id=$('#user_id').val();
        $.ajax({
            url: base_url('api/reSendEmail'),
            type: 'post',
            data: {userId: user_id},
            beforeSend: function () {
                showProgressNoty();
            },
            complete: function () {
                closeProgressNoty();
            },
            success: function (jsonrsp) {
                try {
                    var rsp = $.parseJSON(jsonrsp);
                    noty({type: rsp.type, text: '发送成功！', callback: {
                        afterClose: function () {
                            switch (rsp.type) {
                                case 'success':
                                    noty({
                                        type:'warning',
                                        text:'邮件已发送至您邮箱，现在去激活吗？',
                                        buttons: [
                                            {
                                                addClass: 'btn btn-sm btn-danger', text: '确定',
                                                onClick: function ($noty) {
                                                    $noty.close();
                                                    window.location.href=rsp.message;
                                                }
                                            },
                                            {
                                                addClass: 'btn btn-sm btn-default', text: '取消',
                                                onClick: function ($noty) {
                                                    $noty.close();
                                                }
                                            }
                                        ]
                                    });
                                    break;
                                case 'fail':
                                    noty({type:'error',text:'邮件发送失败，请联系管理员！'});
                                default:
                                    break;
                            }
                        }
                    }});
                } catch (ex) {
                    noty({type: 'error', text: '发生内部错误，请联系管理员'});
                    throw ex;
                }
            }
        });
    });

    $(document).on('click', '.add-school', function () {
        showDialog('addSchool');
    });
    $(document).on('click', '.student-real-name-auth', function () {
        showDialog('studentRealNameAuth');
    });
    $(document).on('click', '.feedback-btn', function () {
        showDialog('feedback');
    });
    $(document).on('click', '.changeschool', function () {
        showDialog('changeschool');
    });

    $(document).on('submit', 'form.search-box', function (event) {
        event.preventDefault();
        var searchStr = $(this).find('input[name=search-str]').val();
        location.href = base_url('home/searchCommodity/?search-str=' + searchStr);
    });
});

function showDialog(dialog, array) {
    var url = base_url('dialog/' + dialog);

    if (dialog == 'editCommodity')
        url += '/' + array['commodityId'];
    if (dialog == 'editRequire')
        url += '/' + array['commodityId'];

    $('.modal-backdrop').remove();

    $.get(url, function (raw) {
        try {
            var rsp = $.parseJSON(raw);
            switch (rsp.type) {
                case 'yime-modal':
                    $('#yime-modal').html(rsp.html);
                    break;
                case 'bootstrap-modal':
                    $('#bootstrap-modal').html(rsp.html);
                    break;
                case 'nopower':
                    noty({
                        type:'warning',
                        text:'请先激活邮箱帐号，并进行实名认证，Lets Go？',
                        buttons: [
                            {
                                addClass: 'btn btn-sm btn-danger', text: '确定',
                                onClick: function ($noty) {
                                    $noty.close();
                                    window.location.href=rsp.html;
                                }
                            },
                            {
                                addClass: 'btn btn-sm btn-default', text: '取消',
                                onClick: function ($noty) {
                                    $noty.close();
                                }
                            }
                        ]
                    });
                default:
                    break;
            }
        } catch (ex) {
            noty({type: 'error', text: '发生内部错误，请联系管理员'});
            throw ex;
        }
    });

    //Modal 垂直居中 动画效果
    $(document).on('shown.bs.modal', '.modal', function (e) {
        var $backdrop = $('.modal-backdrop');
        $backdrop.css({opacity: 0});
        $backdrop.stop().clearQueue().animate({opacity: 0.5});

        var $dialog = $(this).find('.modal-dialog');
        var h = $dialog.height();
        var w = $dialog.width();
        var mt = -(h / 2);
        var ml = -(w / 2);

        if (h < $(window).height()) {
            $dialog.css({
                'position': 'absolute',
                'margin-left': ml,
                'left': '50%',
                'opacity': 0
            });
            $dialog.stop().clearQueue().animate({
                'margin-top': mt,
                'top': '50%',
                'opacity': 1
            });
        } else {
            $dialog.css({
                'opacity': 0
            });
            $dialog.stop().clearQueue().animate({
                'opacity': 1
            });
        }
    });

    $(document).on('hide.bs.modal', '.modal', function (e) {
        var $backdrop = $('.modal-backdrop');
        $backdrop.stop().clearQueue().animate({opacity: 0}, function () {
            $(this).remove();
        });

        var $dialog = $(this).find('.modal-dialog');
        $dialog.stop().clearQueue().animate({
            'top': 0,
            'opacity': 0
        });

        $(this).stop().clearQueue().animate({
            'opacity': 0
        }, function () {
            $('#bootstrap-modal').empty();
            $('#yime-modal').empty();
            $('body').removeClass('modal-open');
            UE.delEditor('commodity-content');
        });

        e.preventDefault();
    });
}

var $progressNoty;

function showProgressNoty() {
    $progressNoty = noty({text: '<div id="progress-noty" class="progress progress-striped active" style="margin:0"><div class="progress-bar" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%"></div></div>', timeout: 0});
}

function changeProgressNoty(percent) {
    $('#progress-noty').find('.progress-bar').css('width', percent + '%');
}

function closeProgressNoty() {
    $progressNoty.close();
}

//统计代码
var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan  style='display:none' id='cnzz_stat_icon_1000335670'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s22.cnzz.com/z_stat.php%3Fid%3D1000335670' type='text/javascript'%3E%3C/script%3E"));